package com.mediastory.spsserver.event.web;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mediastory.spsserver.category.service.CategorySvc;
import com.mediastory.spsserver.event.domain.Event;
import com.mediastory.spsserver.event.service.EventSvc;
import com.mediastory.spsserver.eventFile.service.EventFileSvc;

/**
 * @author Jaewon Choi
 */

@Controller
@RequestMapping(value={"/event"})
public class EventCtrl {
	
	private final Logger logger = Logger.getLogger(getClass());

	@Autowired
	private EventSvc eventSvc;
	
	@Autowired
	EventFileSvc eventFileSvc;
	
	@Autowired
	CategorySvc categorySvc;

	/**
	 * 목록 보기
	 * */
	@RequestMapping(value={"/list.do"})
	public Map<String, Object> list(HttpServletRequest request, HttpSession session) throws Exception {
		Map<String, Object> condition = new HashMap<String, Object>();
		//검색 parameter
		String name = ServletRequestUtils.getStringParameter(request, "name");
		String startDate = ServletRequestUtils.getStringParameter(request, "startDate");
		String endDate = ServletRequestUtils.getStringParameter(request, "endDate");
		String categoryIdx = ServletRequestUtils.getStringParameter(request, "categoryIdx");
		
		condition.put("name", name);
		condition.put("startDate", startDate);
		condition.put("endDate", endDate);
		condition.put("categoryIdx", categoryIdx);

		//페이징 처리 parameter
		int maxnum = ServletRequestUtils.getIntParameter(request, "maxnum", 10);
		int page = ServletRequestUtils.getIntParameter(request, "page", 1);
		condition.put("maxnum", maxnum);
		condition.put("page", (page-1)*maxnum);
		
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.putAll(eventSvc.findbyListPaging(condition));
		resultMap.put("maxnum", maxnum);
		resultMap.put("page", page);
		
		//카테고리 목록
		resultMap.put("categoryList", categorySvc.findbyList(null));
		
		return resultMap;
	}

	/**
	 * 상세 보기
	 * */
	@RequestMapping(value={"/detail.do"})
	public Map<String, Object> detail(HttpServletRequest request, HttpSession session, @RequestParam(required=true, value="idx")  Integer idx) throws Exception {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		
		Map<String, Object> condition = new HashMap<String, Object>();
		condition.put("idx", idx);

		resultMap.put("event", eventSvc.findby(condition));

		//파일 조회
		condition.put("eventIdx", idx);
		//big banner
		condition.put("fileType", "bigBanner");
		resultMap.put("bigBanner", eventFileSvc.findbyList(condition));
		
		//small banner
		condition.put("fileType", "smallBanner");
		resultMap.put("smallBanner", eventFileSvc.findbyList(condition));
		
		//detail
		condition.put("fileType", "detail");
		resultMap.put("detail", eventFileSvc.findbyList(condition));
		
		//메인 이미지 파일 목록
		condition.put("fileType", "mainImg");
		resultMap.put("mainImgs", eventFileSvc.findbyList(condition));
		
		return resultMap;
	}
	
	/**
	 * 등록,수정 폼
	 * */
	@RequestMapping(value={"/form.do"})
	public Map<String, Object> form(HttpServletRequest request, HttpSession session) throws Exception {
		Map<String, Object> resultMap = new HashMap<String, Object>();

		Map<String, Object> condition = new HashMap<String, Object>();
		
		int idx = ServletRequestUtils.getIntParameter(request, "idx", 0);
		if(idx != 0){
			condition.put("idx", idx);
			resultMap.put("event", eventSvc.findby(condition));
			
			//파일 조회
			condition.put("eventIdx", idx);
			//big banner
			condition.put("fileType", "bigBanner");
			resultMap.put("bigBanner", eventFileSvc.findbyList(condition));
			
			//small banner
			condition.put("fileType", "smallBanner");
			resultMap.put("smallBanner", eventFileSvc.findbyList(condition));
			
			//detail
			condition.put("fileType", "detail");
			resultMap.put("detail", eventFileSvc.findbyList(condition));
			
			//메인 이미지 파일 목록
			condition.put("fileType", "mainImg");
			resultMap.put("mainImgs", eventFileSvc.findbyList(condition));			
			
		}
		
		//카테고리 목록
		resultMap.put("categoryList", categorySvc.findbyList(null));

		return resultMap;
	}
	
	/**
	 * 등록/수정
	 * */
	@RequestMapping(value={"/submit.do"}, method=RequestMethod.POST)
	public String submit(Event event, HttpServletRequest request, HttpSession session) throws Exception {
		String userId = (String)session.getAttribute("userId");
		String ip = request.getRemoteAddr();

		if(event.getIdx() != null){
			event.setModifiedId(userId);
			event.setModifiedIp(ip);
			eventSvc.modify(event);
		}else{
			event.setCreatedId(userId);
			event.setCreatedIp(ip);
			eventSvc.add(event);
		}

		return "event/result";
	}
	
	/**
	 * 삭제
	 * */
	@RequestMapping(value={"/remove.do"}, method=RequestMethod.POST)
	public String remove(HttpServletRequest request, HttpSession session) throws Exception {
		String userId = (String)session.getAttribute("userId");
		String ip = request.getRemoteAddr();

		int[] idx = ServletRequestUtils.getIntParameters(request, "idx");
		
		Map<String, Object> condition = new HashMap<String, Object>();
		
		condition.put("modifiedId", userId);
		condition.put("modifiedIp", ip);
		condition.put("idx", idx);
		
		eventSvc.remove(condition);
		return "event/result";
	}
	
	/**
	 * 중복 체크
	 * */
	@RequestMapping(method=RequestMethod.POST, value="/chkDupl")
	public @ResponseBody Map<String, Object> chkContNm(HttpSession session, @RequestParam(required=true, value="idx")  Integer idx, @RequestParam(required=true, value="chkName")  String name) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		try {
			Map<String, Object> condition = new HashMap<String, Object>();
			condition.put("idx", idx);
			condition.put("chkName", name);
			Integer resultCnt = eventSvc.findbyChkNm(condition);
			
			String result = "true";
			if(resultCnt != null && resultCnt > 0){
				result = "false";
			}
			
			resultMap.put("resultCode", "200");
			resultMap.put("result", result);
		} catch (Exception e) {
			// TODO: handle exception
			//e.printStackTrace();
			resultMap.put("resultCode", "500");
			resultMap.put("errMsg", "Error");
		}

		return resultMap;
	}

	/**
	 * 앱 사용 페이지 - 목록 보기
	 * */
	@RequestMapping(value={"/appList"})
	public Map<String, Object> appList(HttpServletRequest request) throws Exception {
		
		int categoryIdx = ServletRequestUtils.getIntParameter(request, "categoryIdx");
		
		Map<String, Object> condition = new HashMap<String, Object>();
		//검색 parameter
		condition.put("isNow", "true");
		condition.put("status", 1);
		
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("eventList", eventSvc.findbyList(condition));
		resultMap.put("eventFileList", eventFileSvc.findbyList(condition));

		return resultMap;
	}
	
	/**
	 * 앱 사용 페이지 - 목록 보기
	 * */
	@RequestMapping(value={"/appList2"})
	public Map<String, Object> appList2(HttpServletRequest request) throws Exception {
		Map<String, Object> condition = new HashMap<String, Object>();
		//검색 parameter
		condition.put("isNow", "true");
		condition.put("status", 1);
		
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("eventList", eventSvc.findbyList(condition));
		resultMap.put("eventFileList", eventFileSvc.findbyList(condition));

		return resultMap;
	}
	
	/**
	 * 이벤트 상세 페이지
	 * */
	@RequestMapping(value={"/appListDetail"}, method=RequestMethod.POST)
	public Map<String, Object> appListDetail(Event event, HttpServletRequest request, HttpSession session) throws Exception {		
		Map<String, Object> resultMap = new HashMap<String, Object>();
		return resultMap;
	}

}